//
//  GRXBuyVC.m
//  1-MushroomStreet-自定义tabbar
//
//  Created by xmg on 15/12/12.
//  Copyright © 2015年 grx. All rights reserved.
//

#import "GRXBuyVC.h"
#import "GRXBuyCollectionViewCell.h"
#import "GRXMarketCell.h"
#import "GRXTenseTimeCell.h"
#import "GRXAllProductCell.h"

#import "GRXShoppingCarController.h"

#import "GRXHeaderRefresh.h"

#import "GRXLoadDataTool.h"

#import "NSString+HTMLExtension.h"
#import "NSDictionary+dictionary.h"

#import <MJExtension/MJExtension.h>
#import "GRXBuyItem.h"
#import "GRXMarketsItem.h"


#import "GRXDetailsController.h"
@interface GRXBuyVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

/**<#注释#>*/
@property (nonatomic, weak) UICollectionView *collectionView;

/**模型数组*/
@property (nonatomic,strong)NSArray *itemArray;

/**数组元素个数*/
@property (nonatomic,assign)NSInteger count;

@end



@implementation GRXBuyVC

static NSString *const marketCell = @"marketCell";
static NSString *const tenseCell = @"tenseCell";
static NSString *const allProductCell = @"allProductCell";
static NSString *const norCell = @"norCell";


static CGFloat const margin = 1;

#pragma mark --- 懒加载




- (NSArray *)itemArray{
    if (_itemArray == nil) {
        _itemArray = [NSArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad{
    [super viewDidLoad];

    
    self.view.backgroundColor = [UIColor lightGrayColor];
//    self.title = @"买买买";
    //设置导航条
    [self setUpNavigation];
  
    [self setCollectionView];
    
    //设置刷新控件
    [self setUpRefresh];
    
    //加载数据
    [self loadData];
    
    //监听跳转通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jumpDetailController:) name:GRXJumpDetailControllerNotification object:nil];
}


//市场跳转
- (void)jumpDetailController:(NSNotification *)info{
    
    NSString *url = info.userInfo[@"url"];
    
    GRXDetailsController *detailController = [[GRXDetailsController alloc] init];
    detailController.url = url;
    
    // 隐藏底部条:条件:在跳转之前设置
    detailController.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:detailController animated:YES];
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark ---集成刷新控件
- (void)setUpRefresh{
    self.collectionView.mj_header = [GRXHeaderRefresh headerWithRefreshingTarget:self refreshingAction:@selector(dataRequest)];
    
    //开始刷新
    [self.collectionView.mj_header beginRefreshing];
}

- (void)dataRequest{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView reloadData];
    });
}


#pragma mark ---数据加载
- (void)loadData{
    
    
    [GRXLoadDataTool loadData:^(NSDictionary *htmlData) {
        self.itemArray = [GRXBuyItem mj_objectArrayWithKeyValuesArray:htmlData[@"banners"]];
    }];
    
}


//创建流水布局
- (void)setCollectionView{

        CGFloat cellW = self.view.width / 2.0;
        CGFloat cellH = 250;

    //1. 创建流水布局
    UICollectionViewFlowLayout *flowLayout = ({
       UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

        //设置流水布局属性
        //2.1 设置cell尺寸
        flowLayout.itemSize = CGSizeMake(cellW, cellH);

        //设置cell间距
        flowLayout.minimumInteritemSpacing = margin;
        flowLayout.minimumLineSpacing = GRXGlableMargin;
   
        flowLayout;
    });
    
    //2.创建collectionView
    UICollectionView *collectionView = ({
        
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) collectionViewLayout:flowLayout];
        collectionView.backgroundColor =  kGRXColor(230, 230, 230);
        collectionView.contentInset = UIEdgeInsetsMake( -10, 0, 0, 0);
        //设置数据源
        collectionView.delegate = self;
        collectionView.dataSource = self;
    
        self.collectionView = collectionView;
    
        collectionView;
    });

        //注册cell
    [collectionView registerNib:[UINib nibWithNibName:@"GRXMarketCell" bundle:nil] forCellWithReuseIdentifier:marketCell];
    [collectionView registerNib:[UINib nibWithNibName:@"GRXTenseTimeCell" bundle:nil] forCellWithReuseIdentifier:tenseCell];
        [collectionView registerNib:[UINib nibWithNibName:@"GRXAllProductCell" bundle:nil] forCellWithReuseIdentifier:allProductCell];
    [collectionView registerNib:[UINib nibWithNibName:@"GRXBuyCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:norCell];



    [self.view addSubview:collectionView];

}

#pragma mark ---UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    

    
    if (section == 0) {
        return 3;
    }else{
        
        return self.itemArray.count;
    }
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
        
        if (indexPath.item == 0) {
            //设置特色市场
            GRXMarketCell *market = [collectionView dequeueReusableCellWithReuseIdentifier:marketCell forIndexPath:indexPath];
            
            return market;
        }else if(indexPath.item == 1){
            
            //设置限时抢购
            GRXTenseTimeCell *tenseTime = [collectionView dequeueReusableCellWithReuseIdentifier:tenseCell forIndexPath:indexPath];
            
            tenseTime.contentView.backgroundColor = [UIColor whiteColor];
            
            return tenseTime;
            
        }else if(indexPath.item == 2){
            //设置全部商品
            GRXAllProductCell *allProduct = [collectionView dequeueReusableCellWithReuseIdentifier:allProductCell forIndexPath:indexPath];
            return allProduct;
        }
    }
    
    //设置全部商品
    GRXBuyCollectionViewCell *normal = [collectionView dequeueReusableCellWithReuseIdentifier:norCell forIndexPath:indexPath];
    
    GRXBuyItem *item = self.itemArray[indexPath.item];
    normal.item = item;
    
    return normal;

}

#pragma mark ---CollectionView代理
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GRXBuyItem *item = self.itemArray[indexPath.item];
    
    NSString *url = item.link;
    
    GRXDetailsController *detailController = [[GRXDetailsController alloc] init];
    detailController.url = url;
    
    // 隐藏底部条:条件:在跳转之前设置
    detailController.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:detailController animated:YES];
    
    NSLog(@"%@",item.link);
}


//定义每个UICollectionViewcell 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        return UIEdgeInsetsMake(10, 0, 10, 0);
    }    
    return UIEdgeInsetsMake(0, 10, 10, 10);
}



- (void)setUpNavigation{
    
    UIBarButtonItem *rightItem = [UIBarButtonItem itemWithNorImage:[UIImage imageNamed:@"btn_cart_black"] highImage:[UIImage imageNamed:@"btn_cart_black"] target:self action:@selector(buyBtnClick)];
    
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 200, 30);
    [btn setImage:[UIImage imageNamed:@"index_search_brand"] forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor grayColor];
    self.navigationItem.titleView = btn;
}


- (void)buyBtnClick{
    NSLog(@"点击了购物车");
    
    GRXShoppingCarController *shopController = [[GRXShoppingCarController alloc] init];
    
    // 隐藏底部条:条件:在跳转之前设置
    shopController.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:shopController animated:YES];
    
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (indexPath.item == 0) {
            return CGSizeMake(kScreenW, 250 + 20);
        }else if(indexPath.item == 1){
            return CGSizeMake(kScreenW, 180);
        }else if(indexPath.item == 2){
            return CGSizeMake(kScreenW, 500 + 20);
        }
    }
    
    return CGSizeMake((kScreenW - 10 * 3) / 2.0, 250);
}




@end
